package com.example.eurekatracea;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaTraceAApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaTraceAApplication.class, args);
	}

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

    @Autowired
    private DiscoveryClient discoveryClient;

	@RequestMapping(value = "/trace-a", method = RequestMethod.GET)
	public String trace() {
		logger.info("===call trace-a===");
        logger.info("Services: " + discoveryClient.getServices());
		return restTemplate().getForEntity("http://trace-b/trace-b", String.class).getBody();
	}

}
